package cn.com.codingce.student.servlet;

import cn.com.codingce.student.entity.Student;
import cn.com.codingce.student.service.IStudentService;
import cn.com.codingce.student.service.impl.StudentServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class AddStudentServlet  业务逻辑层
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int no = Integer.parseInt(request.getParameter("sno"));
        String name = request.getParameter("sname");
        int age = Integer.parseInt(request.getParameter("sage"));
        String address = request.getParameter("saddress");

        Student student = new Student(no, name, age, address);

        IStudentService studentServiceImpl = new StudentServiceImpl();
        boolean result = studentServiceImpl.addStudent(student);
        /*
         * out  request  response session application
         * out: PrintWriter out = response.getWriter();
         * session: request.getSession()
         * application: request.getServletContext();
         *
         */
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();


        if (result) {
            //out.println("增加成功");
            response.sendRedirect("querybypage");
        } else {
            //out.println("添加失败");
            response.sendRedirect("querybypage");
        }
        /*
         * 如果添加失败request域  添加一条数据
         */
		/*if(!result){
			request.setAttribute("error", "addError");
		}else {
			request.setAttribute("error", "noaddError");
		}
		request.getRequestDispatcher("QueryAllStudentsServlet").forward(request, response);*/
    }

}
